package br.edu.univas.jobmanager.view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import br.edu.univas.jobmanager.controller.ControllerPrincipal;
import br.edu.univas.jobmanager.entity.Categoria;
import br.edu.univas.jobmanager.entity.Tarefa;
import br.edu.univas.jobmanager.listener.MenuListener;
import br.edu.univas.jobmanager.listener.TarefaFeitaListener;

public class FramePrincipal extends JFrame implements TarefaFeitaListener{
	private static final long serialVersionUID = 1L;
	
	private ControllerPrincipal _controller;
	private JMenuBar _menuBar;
	private JMenu _arquivoMenu;
	private JMenu _ferramentasMenu;
	private JMenu _ajudaMenu;
	
	private JMenuItem _categoriaItem;
	private JMenuItem _tarefaItem;
	
	
	private JMenuItem _backupItem;
	private JMenuItem _sobreItem;
	
	private JMenuItem _manualItem;
	private JMenuItem _documentacaoItem;
	
	
	private PanelPrincipal _principalPanel;
	private MenuListener _listener;
	public FramePrincipal() {
		super("JobManager - Gerente de Tarefas");
		this._controller = new ControllerPrincipal(this);
		setJMenuBar(getMainMenuBar());
		_listener = _controller.getMenuListener();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		initialize();
		pack();
		//gambiarra para corrigir o bug do windows
		Dimension dimension = getSize();
		dimension.height += 10;
		dimension.width += 10;
		setSize(dimension);
		setResizable(false);
		setLocationRelativeTo(null);
		
	}

	private void initialize() {
		add(getPrincipalPanel());
		
	}

	private JMenuBar getMainMenuBar() {
		if(_menuBar == null){
			_menuBar = new JMenuBar();
			_menuBar.add(getArquivoMenu());
			_menuBar.add(getFerramentasMenu());
			_menuBar.add(getAjudaMenu());
		}
		return _menuBar;
	}

	private JMenu getArquivoMenu() {
		if(_arquivoMenu == null){
			_arquivoMenu = new JMenu("Arquivo");
			_arquivoMenu.add(getTarefaItem());
			_arquivoMenu.add(getCategoriaItem());
		}
		return _arquivoMenu;
	}

	private JMenu getFerramentasMenu() {
		if(_ferramentasMenu == null){
			_ferramentasMenu = new JMenu("Ferramentas");
			_ferramentasMenu.add(getBackupItem());
		}
		return _ferramentasMenu;
	}

	private JMenuItem getCategoriaItem() {
		if(_categoriaItem == null){
			_categoriaItem = new JMenuItem("Categoria");
			_categoriaItem.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					_listener.categoriaAction(_controller);
					
				}
			});
		}
		return _categoriaItem;
	}

	private JMenuItem getTarefaItem() {
		if(_tarefaItem == null){
			_tarefaItem = new JMenuItem("Tarefa");
			_tarefaItem.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					_listener.tarefaAction();
					
				}
			});
		}
		return _tarefaItem;
	}

	private JMenuItem getBackupItem() {
		if(_backupItem == null){
			_backupItem = new JMenuItem("Backup");
			_backupItem.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					_listener.backupAction();
				}
			});
		}
		return _backupItem;
	}

	private JMenuItem getSobreItem() {
		if(_sobreItem == null){
			_sobreItem = new JMenuItem("Sobre");
			_sobreItem.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					JOptionPane.showMessageDialog(FramePrincipal.this, "Desenvolvido por: \nJoão\nDaniel \nRafael\n");
				}
			});
		}
		return _sobreItem;
	}

	private PanelPrincipal getPrincipalPanel() {
		if(_principalPanel == null){
			_principalPanel = new PanelPrincipal(_controller);
			_principalPanel.setFiltroListener(_controller.getFiltroListener());
		}
	
		return _principalPanel;
	}

	@Override
	public Boolean confirm(Tarefa task) {
		Boolean flag;
		String texto = "Deseja marcar a tarefa:\n"+task.getNome()+"\ncomo feita?";
		int retorno = JOptionPane.showConfirmDialog(this, texto,"?", JOptionPane.YES_NO_OPTION);
		if(retorno == JOptionPane.YES_OPTION){
			flag = true;
		}
		else{
			flag = false;
		}
		return flag;
	}

	@Override
	public void clean() {
		getPrincipalPanel().cleanNomeField();
		
	}

	private JMenu getAjudaMenu() {
		if(_ajudaMenu == null){
			_ajudaMenu = new JMenu("Ajuda");
		//	_ajudaMenu.add(getManualItem());
			//_ajudaMenu.add(getDocumentacaoItem());
			_ajudaMenu.addSeparator();
			_ajudaMenu.add(getSobreItem());
		}
		return _ajudaMenu;
	}

	@SuppressWarnings("unused")
	private JMenuItem getManualItem() {
		if(_manualItem == null){
			_manualItem = new JMenuItem("Manual");
			_manualItem.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					_listener.manualAction();
					
				}
			});
		}
		return _manualItem;
	}

	@SuppressWarnings("unused")
	private JMenuItem getDocumentacaoItem() {
		if(_documentacaoItem == null){
			_documentacaoItem = new JMenuItem("Documentacao");
			_documentacaoItem.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					_listener.documentacaoAction();
					
				}
			});
		}
		return _documentacaoItem;
	}


	@Override
	public void refreshComboBox(Categoria categoriaCombo) {
		getPrincipalPanel().atualizaCombo(categoriaCombo);
		
	}
}
